<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="图标地址" prop="icon">
        <el-upload
          :action="upIconUrl"
          list-type="picture-card"
          :auto-upload="true"
          :limit="1"
          :on-success="fileSuccess"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
              >
                <i class="el-icon-download"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="父类ID" prop="pid">
        
        <div class="block">
          <span class="demonstration"></span>
          <el-cascader
            v-model="value"
            :options="options"
            @change="handleChange"
          ></el-cascader>
        </div>

        <!-- <el-input v-model="dataForm.pid" placeholder="父类ID"></el-input> -->
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      value: [],
      options: [
        {
          value: "1",
          label: "家用",
        },
        {
          value: "2",
          label: "办公",
        },
        {
          value: "3",
          label: "水果",
        },
      ],
      visible: false,
      upIconUrl: window.SITE_CONFIG.base + "file/upload",
      dataForm: {
        id: 0,
        name: "",
        icon: "",
        pid: "",
        orderNum: "",
        status: "",
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        icon: [
          { required: true, message: "图标地址不能为空", trigger: "blur" },
        ],
        pid: [{ required: true, message: "父类ID不能为空", trigger: "blur" }],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    getCategoryZtreeData() {
      this.$http({
        url: this.$http.adornUrl("/catgory/treeList"),
        method: "get",
      }).then(({ data }) => {
        this.options = data.map(function (item) {
          return {
            value: item.id,
            label: item.name,
          };
        });
      });
    },
    fileSuccess(response, file, fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.dataForm.icon = response.data;
    },
    init(id) {
      this.getCategoryZtreeData();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/catgory/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.catgory.name;
              this.dataForm.icon = data.catgory.icon;
              this.dataForm.pid = data.catgory.pid;
              this.dataForm.orderNum = data.catgory.orderNum;
              this.dataForm.status = data.catgory.status;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/catgory/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              icon: this.dataForm.icon,
              pid: this.dataForm.pid,
              orderNum: this.dataForm.orderNum,
              status: this.dataForm.status,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
